package com.stadium.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.netflix.discovery.converters.Auto;
import com.stadium.common.utils.SqlMapper;
import com.stadium.common.utils.UserUtils;
import com.stadium.dto.GameEventDto;
import com.stadium.dto.GameScheduleDto;
import com.stadium.entity.GameSchedule;
import com.stadium.mapper.BasicsRecordMP;
import com.stadium.mapper.GameEventMP;
import com.stadium.mapper.GameScheduleMP;
import com.stadium.service.GameScheduleSV;
import com.stadium.vo.GameScheduleVo;
import com.stadium.vo.searchVo.BasicsRecordSearchVo;
import com.stadium.vo.searchVo.GameScheduleSearchVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;

@Service
@Transactional
public class GameScheduleSVI implements GameScheduleSV {
    @Autowired
    private GameScheduleMP mapper;
    @Autowired
    private SqlMapper sqlMapper;
    @Autowired
    private GameEventMP gameEventMP;
    @Autowired
    private BasicsRecordMP basicsRecordMP;

    @Override
    public List<GameSchedule> queryBySQL(String sql, Map<String, Object> map) {
        return sqlMapper.selectList(sql, map, GameSchedule.class);
    }

    @Override
    public void save(GameScheduleVo gameScheduleVo) {
        GameSchedule gameSchedule = new GameSchedule();
        BeanUtils.copyProperties(gameScheduleVo, gameSchedule);
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        gameSchedule.setCreateUser(UserUtils.getUserId());
        gameSchedule.setUpdateUser(UserUtils.getUserId());
        gameSchedule.setId(id);
        mapper.save(gameSchedule);
    }

    @Override
    public void update(GameScheduleVo gameScheduleVo) {
        GameSchedule gameSchedule = new GameSchedule();
        BeanUtils.copyProperties(gameScheduleVo, gameSchedule);
        gameSchedule.setUpdateUser(UserUtils.getUserId());
        mapper.update(gameSchedule);
    }

    @Override
    public void saveBatch(List<GameSchedule> list) {
        mapper.saveBatch(list);
    }

    @Override
    public void delete(String id) {
        mapper.delete(id);
    }

    @Override
    public void deleteBatch(String[] ids) {
        mapper.deleteBatch(ids);
    }

    @Override
    public List<GameScheduleDto> query(GameScheduleSearchVo searchVo) {
        List<GameScheduleDto> gameScheduleDtoList =  mapper.query(searchVo);
        long time = new Date().getTime();
        gameScheduleDtoList.stream().forEach(gameScheduleDto -> {
            BasicsRecordSearchVo basicsRecordSearchVo = new BasicsRecordSearchVo();
            basicsRecordSearchVo.setScheduleId(gameScheduleDto.getId());
            int count = basicsRecordMP.getCount(basicsRecordSearchVo);

            if (gameScheduleDto.getCompetitionStarttime().getTime() > time) {
                gameScheduleDto.setCompetitionStauts("0");
            } else if (gameScheduleDto.getCompetitionStarttime().getTime() < time && gameScheduleDto.getCompetitionEndtime().getTime() > time) {
                gameScheduleDto.setCompetitionStauts("1");
            }
            if (count > 0) {
                gameScheduleDto.setCompetitionStauts("2");
            }
        });
        return gameScheduleDtoList;
    }

    @Override
    public GameScheduleDto get(String id) {
        GameScheduleDto gameScheduleDto = mapper.get(id);
        GameEventDto gameEventDto = gameEventMP.get(gameScheduleDto.getEventId());
        gameScheduleDto.setRuleId(gameEventDto.getRuleId());
        long time = new Date().getTime();
        if (gameScheduleDto.getCompetitionStarttime().getTime() > time) {
            gameScheduleDto.setCompetitionStauts("0");
        } else if (gameScheduleDto.getCompetitionStarttime().getTime() < time && gameScheduleDto.getCompetitionEndtime().getTime() > time) {
            gameScheduleDto.setCompetitionStauts("1");
        } else if (gameScheduleDto.getCompetitionEndtime().getTime() < time) {
            gameScheduleDto.setCompetitionStauts("2");
        }
        return gameScheduleDto;
    }

    @Override
    public int getCount(GameScheduleSearchVo searchVo) {
        return mapper.getCount(searchVo);
    }

}
